<?Php
class User_V2_Guest extends User_V2_Base
{
    public function __construct ($uid = NULL, $userinfo = NULL)
    {
        parent::__construct(0, Array('userIcon' => '/resource/skin/img/login_touxiang.png'));
    }

    public function sendMobileCode ($mobile)
    {
        $code = $this->getMobileCode($mobile);
        if (! empty($code))
        {
            $msg = "【点睛时创】欢迎注册，您的验证码是$code";
            Util_SMSYP::send($mobile, $msg);
        }
        else
        {
            //
        }
    }

    public function login ($mobile, $password, $type = 'WEB',$utype=NULL)
    {
        //
        $query = DB::select()
            ->from('user_v2')
            ->where_open()
            ->where('userId', '=', $mobile)
            ->or_where('telNum','=',$mobile)
            ->where_close()
            ->where('userToken', '=', md5($password))
            ->where('source', '=', $type)
            ->where('utype','=',$utype)
            ->execute();

        if ($query->count() == 1)
        {
            $curr = $query->current();
            if( 99 != $curr['status']){
                $uid  = $curr['uid'];
                Passport::setCurrentUserUid($uid);
                return $uid;
            }else{
                Passport::setCurrentUserUid(0);
                return FALSE;
            }

        }
        else
        {
            Passport::setCurrentUserUid(0);
            return FALSE;
        }
    }
    
    public function register ($mobile, $code, $password)
    {
        if (! $this->validMobileCode($mobile, $code))
            throw new Http_Exception_404('手机认证码失败');


        $source = 'mobile';
        $userId = $mobile;
        $userToken= md5($password);
        $userName = '手机用户_'.substr($mobile, 7, 4);
        $userIcon = '/resource/skin/img/login_touxiang.png';
        $userGender = 'm';
        $userPushId = '';

        $user = user::initUserFromBasicInfo ($source, $userId, $userToken, $userName, $userIcon, $userGender, $userPushId);
        $uid  = $user->getUserUid();
        $icon = $user->getUserIcon();
        $push = $user->getUserPushId();

        Passport::setCurrentUserUid ($uid);
        return $uid;
    }

    public function forget ($mobile, $code, $password)
    {
        if (! $this->validMobileCode($mobile, $code))
            throw new Http_Exception_404('手机认证码失败');


        $source = 'mobile';
        $userId = $mobile;
        $userToken= md5($password);
        $userName = '手机用户_'.substr($mobile, 7, 4);
        $userIcon = '/resource/skin/img/login_touxiang.png';
        $userGender = 'm';
        $userPushId = '';
        
        $query = DB::select()
            ->from('user_v2')
            ->where('source', '=', $source)
            ->where('userid', '=', $userId)
            ->execute();

        if ($query->count() > 0)
        {
            $current = $query->current();
            $uid     = $current['uid'];
            $status  = $current['status'];
            if ($status != 1){
                $user = new User_v2_Guest();
            }
            else
            {
                $user = new User_v2_Member($uid, $current);
                $user->updateUserPushId($userPushId);
                
                $rows = DB::update('user_v2')
                    ->set(array('userToken' => $userToken))
                    ->where('uid', '=', $uid)
                    ->execute();
                
            }
            
            $uid  = $user->getUserUid();
            $icon = $user->getUserIcon();
            $push = $user->getUserPushId();

            Passport::setCurrentUserUid ($uid);
            return $uid;
            
        }else{
            return false;
        }

    }

    public function validMobileCode ($mobile, $code)
    {
        // 以服务器时间为准
        $tMinTime = time() - 3600;

        $code2 = DB::select('code')
            ->from('user_v2_mobile_code')
            ->where('mobile', '=', $mobile)
            ->where('created', '>=', $tMinTime)
            ->limit(1)
            ->order_by('created', 'desc')
            ->execute()
            ->get('code');

        if (! empty($code2) && $code == $code2)
            return TRUE;
        else
            return FALSE;
    }

    private function getMobileCode ($mobile)
    {
        // 以服务器时间为准
        $tNow  = time();

        $query = DB::select()
            ->from('user_v2_mobile_code')
            ->where('mobile', '=', $mobile)
            ->execute();


        foreach ($query as $rs)
        {
            // 有小于60秒得记录, 按失败处理
            if (($tNow -  $rs['created']) <= 60)
                return FALSE;
        }
        
        $code = $this->getRandomCode();
        $row  = array(
            'mobile' => $mobile,
            'code'   => $code,
            'created'=> $tNow,
        );

        list($insert_id, $rows) = DB::insert('user_v2_mobile_code')
            ->columns (array_keys($row))
            ->values (array_values($row))
            ->execute();

        if ($insert_id > 0)
            return $code;
        else
            return FALSE;
    }

    private function getRandomCode ()
    {
        return  rand(0,9).
                rand(0,9).
                rand(0,9).
                rand(0,9).
                rand(0,9).
                rand(0,9);
    }
}
